package com.zytool.easyrestful.core;

import com.zytool.easyrestful.core.bean.ResultBean;
import com.zytool.easyrestful.core.utils.HttpRequestUtil;
import io.swagger.annotations.ApiOperation;
import lombok.Setter;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;

public class GenericController<T extends GenericEntity> {

    @Setter
    private GenericService<T> genericService;

    @ApiOperation("查询列表")
    @GetMapping("/list")
    public Object list(Pageable pageable, HttpServletRequest request) {
        return ResultBean.success(genericService.selectByPage(pageable, HttpRequestUtil.getRequestMap(request)));
    }

    @ApiOperation("查询详情")
    @GetMapping("/get")
    public Object get(@RequestParam int id) {
        return ResultBean.success(genericService.getById(id));
    }

    @ApiOperation("保存")
    @PostMapping("/save")
    public Object save(@RequestBody T entity) {
        return ResultBean.success(genericService.save(entity));
    }

    @ApiOperation("删除")
    @PostMapping("/delete")
    public Object delete(@RequestBody T entity) {
        return ResultBean.success(genericService.delete(entity));
    }
}
